class VoteEntity {
  final String voteId;
  final String description;
  final int voteCount;
  final List<String> votedUserIds;
  final DateTime createdTime;

  const VoteEntity({
    required this.voteId,
    required this.description,
    required this.voteCount,
    required this.votedUserIds,
    required this.createdTime,
  });

  factory VoteEntity.empty() {
    return VoteEntity(
      voteId: '',
      description: '',
      voteCount: 0,
      votedUserIds: [],
      createdTime: DateTime.now(),
    );
  }

  factory VoteEntity.fromJson(Map<String, dynamic> json) {
    return VoteEntity(
      voteId: json['voteId'] ?? '',

      description: json['description'] ?? '',
      voteCount: json['voteCount'] ?? 0,
      votedUserIds:
          (json['votedUserIds'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      createdTime:
          json['createdTime'] != null
              ? DateTime.parse(json['createdTime'])
              : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'voteId': voteId,
      'description': description,
      'voteCount': voteCount,
      'votedUserIds': votedUserIds,
      'createdTime': createdTime.toIso8601String(),
    };
  }

  VoteEntity copyWith({
    String? voteId,
    String? description,
    int? voteCount,
    List<String>? votedUserIds,
    DateTime? createdTime,
  }) {
    return VoteEntity(
      voteId: voteId ?? this.voteId,
      description: description ?? this.description,
      voteCount: voteCount ?? this.voteCount,
      votedUserIds: votedUserIds ?? this.votedUserIds,
      createdTime: createdTime ?? this.createdTime,
    );
  }

  bool get isValid => voteId.isNotEmpty && description.isNotEmpty;

  bool hasUserVoted(String userId) => votedUserIds.contains(userId);

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is VoteEntity && other.voteId == voteId;
  }

  @override
  int get hashCode => voteId.hashCode;

  @override
  String toString() {
    return 'VoteEntity(voteId: $voteId, description: $description, voteCount: $voteCount)';
  }
}
